<?php
/*
 * 特卖会 搜索
 */

if (!defined('IN_SEXMALL')){
	die('Hacking attempt');
}

$keywords 	= !empty($_REQUEST['keywords'])? htmlspecialchars(trim($_REQUEST['keywords'])):'';
$brandid  	= !empty($_REQUEST['bid']) ? intval($_REQUEST['bid']) : 0;
$cateid		= !empty($_REQUEST['cid']) ? intval($_REQUEST['cid']) : 0;
$show 		= empty($_REQUEST['show']) ? (wap_getcookie("WAP[show]") ? wap_getcookie("WAP[show]") : 2) : intval($_REQUEST['show']);
$sort 		= empty($_REQUEST['sort']) ? 4 : intval($_REQUEST['sort']);
$filter_stock = empty($_REQUEST['filter_stock']) ? 0 : intval($_REQUEST['filter_stock']);	// 过滤

if (empty($keywords)){
	show_message('请输入搜索关键字', '返回', 'index.html', 'warning');
}

// 定义产品图片尺寸
if ($show == 2){
	$pic_format = array('w_h'=>'138x174', 'ratio'=>70);
	$isHD && $pic_format = array('w_h'=>'276x348', 'ratio'=>80);
}
else{
	$pic_format = array('w_h'=>'90x113', 'ratio'=>70);
	$isHD && $pic_format = array('w_h'=>'180x226', 'ratio'=>80);
}

$cache_id = sprintf('%X', crc32(__FILE__));
if (!$smarty->is_cached('search.dwt', $cache_id))
{

	// 获得品牌信息
	/*$brand_list = $mem->getCache('sexmall_channel_0', true);
	 if (empty($brand_list) || !is_array($brand_list)){
		$brand_list = $mInterface->getBrandSelling();
		$mem->addCache('sexmall_channel_0', $brand_list, BRAND_CACHE_EXPIRE, true);
		}*/

	$brand_list = get_channel_brand_list(0);

	// 读取搜索结果
	$result = $goods_sel = $brand = $category = $tmp_category = array();
	$cache_name = 'sexmall_search_'.urlencode($keywords);
	$result = $mem->getCache($cache_name, true);
	if(!$result) {
		$result = $mInterface->getUserSearch($keywords);
		$mem->addCache($cache_name, $result, PRODUCT_LIST_CACHE_EXPIRE, true);
	}

	if (!empty($result) && is_array($result))
	{
		foreach ((array)$result AS $key => $val)
		{
			if ($filter_stock && $val['sale_out']){
				unset($result[$key]);
				continue;
			}

			//获取商品分类
			$tmp_category[] = $val['product_cat_id'];

			//获取商品品牌
			$tmp_brand[] = $val['brand_id'];

			//格式化产品列表图片
			$result[$key]['small_image2'] = get_small_image($val['small_image'], $pic_format);

			if($brandid && $val['brand_id']==$brandid)
			{
				$goods_sel1[] = $result[$key];
			}

			if($cateid && $val['product_cat_id']==$cateid)
			{
				$goods_sel2[] = $result[$key];
				$tmp_brand2[] = $val['brand_id'];
			}
		}

		//取到产品的品牌列表
		if($cateid)$tmp_brand=$tmp_brand2;

		$tmp_brand = array_unique($tmp_brand);
		foreach ($brand_list AS $a=>$b)
		{
			foreach ($tmp_brand as $bid)
			{
				if ($b['brand_id'] == $bid)
				{
					$brand[] = array('brand_id'=>$b['brand_id'],'brand_name'=>$b['brand_name']);

					$catInfo = get_brand_category($bid);
					$cate = $catInfo['categorys'];

					foreach ($cate as $c)
					{
						$category[$c['cat_id']] = $c['cat_name'];
					}
					unset($brand_list[$a]);
					break;
				}
			}
		}

		$brandid && $result = $goods_sel1;
		$cateid && $result = $goods_sel2;
		if($brandid && $cateid)$result = array_intersect($goods_sel1, $goods_sel2);


		//品牌取分类
		if($brand)
		{
			if($category)
			{
				$category = array_unique($category);
				//过滤掉商品没有的分类
				$tmp_c = array();
				foreach ($tmp_category as $cid)
				{
					foreach ($category as $k=>$c)
					{
						if($cid==$k){
							$tmp_c[$k] = $c;
						}
					}
				}
				if($tmp_c)$category =$tmp_c;
			}
		}

		// 排序
		switch ($sort)
		{
			case 1:	//价格由低到高
				foreach ((array)$result AS $key => $val){
					$volume[$key]  = $val['sexmall_price'];
				}
				array_multisort($volume, SORT_ASC, $result);
				break;
				//		case 2: //价格从高到低
				//			foreach ((array)$result AS $key => $val){
				//				$volume[$key]  = $val['sexmall_price'];
				//			}
				//			array_multisort($volume, SORT_DESC, $result);
				//			break;
			case 3: //折扣由低到高
				foreach ((array)$result AS $key => $val){
					$volume[$key]  = floatval($val['agio']);
				}
				array_multisort($volume, SORT_ASC, $result);
				break;
			case 4: //按品牌分类
				if($brand)
				{
					$ls = $brand;
					foreach ((array)$brand AS $kd=>$bd){
						foreach ((array)$result AS $key => $val){
							if($bd['brand_id']==$val['brand_id'])$ls[$kd]['gd'][] = $val;
						}
					}
					$result = $ls;
				}
			default:
				;
		}

	}
	else{
		$result = array();
	}

	/* OMNITURE 统计  */
	include 'omniture/search.php';

	$smarty->assign('brandid', 		$brandid);
	$smarty->assign('brand', 		$brand);
	$smarty->assign('cateid', 		$cateid);
	$smarty->assign('category', 	$category);
	$smarty->assign('goods_list', 	$result);
	$smarty->assign('keywords', 	$keywords);
	$smarty->assign('sort', 		$sort);
	$smarty->assign('show', 		$show);
	$smarty->assign('filter_stock', 		$filter_stock);

	$smarty->assign('title', '搜索');
}

$smarty->display('search.dwt');

?>